package com.shishuo.cdn.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shishuo.cdn.dao.UserBankLogDao;
import com.shishuo.cdn.dao.UserDao;
import com.shishuo.cdn.entity.User;

/**
 * 资金管理
 * @author liaoliyun
 *
 */

@Controller
@RequestMapping("/fund")
public class FundAction extends BaseAction {
	private static final int DEFAULT_COUNT_ONEPAGE=5;//默认每页显示条数
	@Autowired
	private UserDao userDao;
	@Autowired
	private UserBankLogDao userBankLogDao;
	
	@RequestMapping(value="/fund.htm")
	public String IDCService(HttpServletRequest request,ModelMap modelMap
			){
		User user=(User)request.getSession().getAttribute("user");
		user=userDao.getUserById(user.getReg_tel());
		
		createParameterMap(request);
		int pageNo=paramsMap.get("pageNo")!=null?Integer.valueOf(paramsMap.get("pageNo").toString()):1;
//		-------分页查询
		pageNo=pageNo<1?1:pageNo;//页码当前为1
//		pageNo=paramsMap.get("pageNo")!=null?Integer.valueOf(paramsMap.get("pageNo").toString()) :1;//第几页
		if("".equals(paramsMap.get("bank_type")))paramsMap.remove("bank_type");//""查询全部
		if("".equals(paramsMap.get("start_date")))paramsMap.remove("start_date");//
		if("".equals(paramsMap.get("end_date")))paramsMap.remove("end_date");//
		
		paramsMap.put("status", 1);//已完成的交易
		int totalCount=userBankLogDao.queryuserBankLogCount(paramsMap);//数据总数
		//总页数
		int pages=totalCount%DEFAULT_COUNT_ONEPAGE==0?totalCount/DEFAULT_COUNT_ONEPAGE:totalCount/DEFAULT_COUNT_ONEPAGE+1;
//		if(pageNo<1) pageNo=1;
		if(pageNo>pages) pageNo=pages;
		int offset=(pageNo-1)*DEFAULT_COUNT_ONEPAGE;//偏移量
		
		if(offset<0)offset=0;
		paramsMap.put("offset", offset);
		paramsMap.put("rows", DEFAULT_COUNT_ONEPAGE);
		
		
		List<Map<String,Object>> list=userBankLogDao.queryuserBankLog(paramsMap);
		paramsMap.put("dataList", list);
		modelMap.putAll(paramsMap);
		modelMap.put("pages",pages);//总页数
		modelMap.put("totalCount", totalCount);//总条数
		modelMap.put("pageNo", pageNo);//当前页号
	
		modelMap.put("balance", user.getBalance());
		modelMap.put("myaccount", user.getReg_tel());
		
		return "/business/fund";//ftl文件路径
	}
	
	/**
	 * 
	 * 描述：jy明细
	 * 创建人： 魏泽超
	 * 创建时间: 2016-4-1 上午11:05:43
	 * @version
	 * @throws ParseException 
	 */
	@RequestMapping(value="/jydetail.htm")
	public String jydetail(HttpServletRequest request,ModelMap modelMap
			) throws ParseException{
		User user=(User)request.getSession().getAttribute("user");
		user=userDao.getUserById(user.getReg_tel());
		
		createParameterMap(request);
		int pageNo=paramsMap.get("pageNo")!=null?Integer.valueOf(paramsMap.get("pageNo").toString()):1;
//		-------分页查询
		pageNo=pageNo<1?1:pageNo;//页码当前为1
//		pageNo=paramsMap.get("pageNo")!=null?Integer.valueOf(paramsMap.get("pageNo").toString()) :1;//第几页
		if("".equals(paramsMap.get("bank_type")))paramsMap.remove("bank_type");//""查询全部
		if("".equals(paramsMap.get("start_date")))paramsMap.remove("start_date");//
		if("".equals(paramsMap.get("end_date")))paramsMap.remove("end_date");//
		String sTime=request.getParameter("sTime");
		String eTime=request.getParameter("eTime");
		if(!("").equals(sTime) && !("").equals(eTime) && sTime !=null && eTime !=null){
			SimpleDateFormat sd=new SimpleDateFormat("MM/dd/yyyy");
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			Date old=sd.parse(sTime);
			Date olde=sd.parse(eTime);
			paramsMap.put("start_date", sdf.format(old)+" 00:00:00");
			paramsMap.put("end_date", sdf.format(olde)+" 23:59:59");
		}
		
		
		paramsMap.put("status", 1);//已完成的交易
		int totalCount=userBankLogDao.queryuserBankLogCount(paramsMap);//数据总数
		//总页数
		int pages=totalCount%DEFAULT_COUNT_ONEPAGE==0?totalCount/DEFAULT_COUNT_ONEPAGE:totalCount/DEFAULT_COUNT_ONEPAGE+1;
//		if(pageNo<1) pageNo=1;
		if(pageNo>pages) pageNo=pages;
		int offset=(pageNo-1)*DEFAULT_COUNT_ONEPAGE;//偏移量
		
		if(offset<0)offset=0;
		paramsMap.put("offset", offset);
		paramsMap.put("rows", DEFAULT_COUNT_ONEPAGE);
		
		List<Map<String,Object>> list=userBankLogDao.queryuserBankLog(paramsMap);
		paramsMap.put("dataList", list);
		modelMap.putAll(paramsMap);
		modelMap.put("pages",pages);//总页数
		modelMap.put("totalCount", totalCount);//总条数
		modelMap.put("pageNo", pageNo);//当前页号
		modelMap.put("sTime", sTime);//开始时间
		modelMap.put("eTime", eTime);//结束时间
	
		modelMap.put("balance", user.getBalance());
		modelMap.put("myaccount", user.getReg_tel());
		
		return "/business/jydetail";//ftl文件路径
	}

}
